package com.cxb.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * @Classname AsyncService
 * @Date 2023/8/18 22:41
 * @Created by Administrator
 * @Description TODO
 */
@Service
public class AsyncService {

    @Autowired
    @Qualifier("taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    public CompletableFuture<String> asyncMethod() {
        return CompletableFuture.supplyAsync(() -> {
            // 异步执行的任务
            return "任务执行结果";
        });
    }

    public void executeAsyncTask() {
        taskExecutor.execute(() -> {
            // 异步执行的任务
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步执行的任务");
        });
    }
}
